<template>
  <div class="c-bg-sgray" v-if="dataInfo != null">
    <div class="c-w100 c-maxh427 c-text-hidden">
      <img class="c-w100 img-pe-none" :src="$addXossFilter(dataInfo.avatar || 'https://ck-bkt-knowledge-payment.oss-cn-hangzhou.aliyuncs.com/admin/material/9_material_admin/image/assets/i/wap/newAreaAgent/server.png')" alt="">
    </div>
    <div class="c-bg-white c-pv24 c-flex-column">
      <div class="c-fs30 c-fw-b c-ph24">{{dataInfo.name}}</div>
      <price-bar class="c-mt10 c-ph24" :showPrice="Number(dataInfo.prodPrice)"></price-bar>
    </div>
    <div class="c-mt12 c-bg-white c-ph24 c-pv24">
      <div class="c-fs24 c-fw-b">简介</div>
      <div class="c-mt20">
        <course-description :independentCopy="true" :independentCopyPicture="true" ref="descriptions" :prodType="dataInfo.prodType" :prodId="0"></course-description>
      </div>
    </div>
    <div class="c-flex-row c-ph24 c-aligni-center c-justify-sb c-bd-t1 c-hh88 c-pf c-p-b0 c-w100 c-maxw640 c-bg-white c-pz1" v-if="dataInfo.enableOnlineUpgrade && !$isWxAppIos()"> <!-- enableOnlineUpgrade : 1可以， 0不可以-->
      <div class="c-fs28 c-lh28 c-pv18 c-fc-white c-textAlign-c c-br36 theme-bg c-w100" @click="goPay">
        立即购买
      </div>
    </div>
  </div>
</template>

<script>
import courseDescription from '@/components/templates/courseDescription';
import PriceBar from '@/components/templates/common/priceBar.vue';
import { getBindPhoneSetting } from "@/utils/checkUnBindPhone.js";
import { utilJs } from "@/utils/common.js"
let isLoadding = false;
let isFromCoursePay = false;
export default {
  name: "AgentDesc",
  components: {
    courseDescription,
    PriceBar
  },
  data() {
    return {
      theme: localStorage.getItem("colorName") ? localStorage.getItem("colorName") : 'mb5_default',
      companyAuth: JSON.parse(localStorage.getItem("companyAuth")),
      dataInfo: null,
      applyType: -1,
      bindPhoneSetting: null,
      goodsRoomId: '', // 直播间id，从直播间来到这个页面
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (from.path == "/homePage/course/coursePay") {
        isFromCoursePay = true;
      } else {
        isFromCoursePay = false;
      }
    });
  },
  computed: {
  },
  created() {
    utilJs.appShare(this);
  },
  activated() {
    setDocumentTitle("服务商简介");
    isLoadding = false;
    this.dataInfo = null;
    this.goodsRoomId = this.$route.query.goodsRoomId || '';
    this.applyType = this.$route.query.applyType;
    let cf = (this.applyType == 3 ? global.ckFrom.agentApply : this.applyType == 5 ? global.ckFrom.partnerApply : this.applyType == 6 ? global.ckFrom.branchApply : this.applyType == 7 ? global.ckFrom.cooApply : this.applyType == 8 ? global.ckFrom.agencyApply : this.applyType == 9 ? global.ckFrom.agent6Apply : this.applyType == 10 ? global.ckFrom.agent7Apply : this.applyType == 11 ? global.ckFrom.agent8Apply : global.ckFrom.agent9Apply);
    getBindPhoneSetting(cf).then(res => {
      this.bindPhoneSetting = res;
    });
    this.getDetail();
  },
  methods: {
    getDetail() {
      utilJs.getMethod(`${global.apiurl}Agent/getAgentProdInfo?applyType=${this.applyType}`, res => {
        if (!res || res.length == 0) {
          this.$routerGo(this, "replace", { path: "/error/emptyPage", query: { from: 'agent', info: '此商品已删除，请联系管理员！' } });
          return;
        }
        this.dataInfo = res;
        if (isFromCoursePay && !this.dataInfo.enableOnlineUpgrade) {
          let that = this;
          this.$cjConfirm({
            title: '', //
            titleCenter: true, //标题是否居中
            message: '服务商权限已开通，为更好享受您的权益，请及时完善您的基础信息',
            messageCenter: true, //内容是否居中
            showMasker: true, //是否显示透明背景
            clickCloseModal: false, //是否点击透明背景关闭弹窗
            showCancelButton: true, //是否显示取消按钮
            cancelButtonText: '取消', //取消按钮文本
            confirmButtonText: '去完善资料', //确认按钮文本
            onConfirm() {
              that.$routerGo(that, 'push', { path: `/homePage/agent/agentDetail?listType=1` });
            }, //确认事件回调
            onCancel() {} //取消事件回调
          });
        }
        this.$nextTick(function () {
          this.$refs.descriptions.getDescptionDetail();
        });
        this.wechatShare();
      });
    },
    spreadInvite: function () {
      this.$routerGo(this, "push", {
        path: `/homePage/spread/spreadAgent?&ckFrom=1`,
      });
    },
    //点击购买
    goPay() {
      if (this.bindPhoneSetting && this.bindPhoneSetting.buyBind && utilJs.goBindMobile()) {return;}
      if (isLoadding) {
        return;
      }
      isLoadding = true;
      let data = null;
      data = {
        applyType: this.applyType,
        applyStatus: 2, // 线上付费
        from: 4, // 是购买页进入
      };
       utilJs.postMethod(`${global.apiurl}members/applySave`, data, (res) => {
        if (!utilJs.isOpenIosGzhPay(global.ckFrom.agentApply)) {
          isLoadding = false;
          this.$store.commit('updateShowVitualModal', true);
          return;
        }
        isLoadding = false;
        let data = { cId: -1, extId: this.applyType, ckFrom: this.dataInfo.prodType, isFromInvite: 0, aaId: res.aaId, protocolId: '' };
        if (this.goodsRoomId) {
          data.goodsRoomId = this.goodsRoomId;
        }
        this.$routerGo(this, "push", {
          path: '/homePage/course/coursePay',
          query: data
        });
       }, failRes => {
        isLoadding = false;
       });
    },
    //分享
    wechatShare: function () {
      let title = this.dataInfo.name;
      let shareImg = this.dataInfo.avatar || 'https://ck-bkt-knowledge-payment.oss-cn-hangzhou.aliyuncs.com/admin/material/9_material_admin/image/assets/i/wap/newAreaAgent/server.png'
      let shareUrl = `${window.location.href.split("#")[0]}#/homePage/agent/agentDesc?applyType=${this.applyType}&refereeId=${localStorage.getItem("userId")}`;
      let shareDesc = "点击查看详情";
      utilJs.wechatConfig(shareUrl, title, shareImg, shareDesc, function () { }, true);
    },
    //手机端分享
    appShare: function () {
      let title = this.dataInfo.name;
      let shareImg = this.dataInfo.avatar || 'https://ck-bkt-knowledge-payment.oss-cn-hangzhou.aliyuncs.com/admin/material/9_material_admin/image/assets/i/wap/newAreaAgent/server.png'
      let shareUrl = `${window.location.href.split("#")[0]}#/homePage/agent/agentDesc?applyType=${this.applyType}&refereeId=${localStorage.getItem("userId")}`;
      let shareDesc = "点击查看详情";
      utilJs.appShareTrue(title, shareImg, shareUrl, shareDesc);
    },
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
